﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.System.Menu;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using ManagementApi.Mvc.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.System.Menu;

namespace ManagementApi.Controllers.SystemManage
{
    /// <summary>
    /// 菜单相关业务接口
    /// </summary>
    [Route("/api/v1/menus")]
    [ApiController]
    [Authorize(Roles = "SysUser")]
    public class MenuController : ControllerBase
    {
        private readonly SystemUserApplicationContext applicationContext;
        private readonly MenuService menuService;
        private readonly IMapper mapper;
        private readonly IConfiguration configuration;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="applicationContext"></param>
        /// <param name="menuService"></param>
        /// <param name="mapper"></param>
        /// <param name="configuration"></param>
        public MenuController(
            SystemUserApplicationContext applicationContext,
            MenuService menuService,
            IMapper mapper,
            IConfiguration configuration
            )
        {
            this.applicationContext = applicationContext;
            this.menuService = menuService;
            this.mapper = mapper;
            this.configuration = configuration;
        }

        /// <summary>
        /// 菜单表单数据
        /// </summary>
        /// <param name="id">菜单ID</param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        public ApiResult<MenuEditForm> GetMenu(int id)
        {
            var menu = menuService.GetSysMenu(id);
            if (menu == null)
                return ApiResult<MenuEditForm>.Failure("菜单数据不存在");
            MenuEditForm menuForm = new MenuEditForm();
            mapper.Map(menu, menuForm);
            return ApiResult<MenuEditForm>.Success(menuForm);
        }

        /// <summary>
        /// 菜单列表
        /// </summary>
        /// <param name="keywords">搜索条件</param>
        /// <returns></returns>
        [HttpGet("listMenus")]
        public ApiResult<IEnumerable<MenuDto>> GetListMenus(string? keywords)
        {
            var menus = menuService.GetAllMenus().AsMenus(keywords);
            return ApiResult<IEnumerable<MenuDto>>.Success(menus);
        }

        /// <summary>
        /// 菜单下拉树
        /// </summary>
        /// <returns></returns>
        [HttpGet("menuOptions")]
        public ApiResult<IEnumerable<VueTree<int>>> GetMenuOptions()
        {
            var menus = menuService.GetAllMenus().AsOptions();
            return ApiResult<IEnumerable<VueTree<int>>>.Success(menus);
        }

        /// <summary>
        /// 创建菜单
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("create")]
        [PermissionCheck("sys:menu:add")]
        public ApiResult Add(MenuEditForm form)
        {
            SysMenu menu = new SysMenu();
            mapper.Map(form, menu);
            menu.CreateTime = DateTime.Now;
            var list = menuService.GetAllMenus();
            menu.TreePath = menuService.GetTreePath(form.ParentId, form.ParentId, list);
            menuService.CreateMenu(menu);
            return ApiResult.Success("创建成功");
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [PermissionCheck("sys:menu:edit")]
        public ApiResult Update(int id, MenuEditForm form)
        {
            var menu = menuService.GetSysMenu(id);
            if (menu == null) return ApiResult.Failure("菜单不存在");
            mapper.Map(form, menu);
            menu.MenuId = id;
            menu.UpdateTime = DateTime.Now;
            var list = menuService.GetAllMenus();
            menu.TreePath = menuService.GetTreePath(form.ParentId, form.ParentId, list);
            menuService.UpdateMenu(menu);
            return ApiResult.Success("编辑成功");
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        [PermissionCheck("sys:menu:delete")]
        public ApiResult Delete(int id)
        {
            var menu = menuService.GetSysMenu(id);
            if (menu == null) return ApiResult.Failure("菜单不存在");
            menuService.DeleteMenu(id);
            return ApiResult.Success("删除成功");
        }
    }
}